# -*- coding: utf-8 -*-
import scrapy
import redis
import json

class Id97Spider(scrapy.Spider):
    name = 'id97'
    allowed_domains = ['www.id97.com']
    start_urls = ['http://www.id97.com/movie/top250_douban']
    redis_pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
    myredis = redis.Redis(connection_pool=redis_pool)

    def parse(self, response):
        title = response.xpath('//div[@class="col-xs-9"]/h4/a/text()').extract()
        next_page = response.xpath('//ul[@class="pagination pagination-sm"]/li[last()-1]/a/@href').extract_first()
        url = response.urljoin(next_page)
        self.myredis.hset('CACHE_TOP_250_DOUBAN',next_page json.dumps(title))
        yield scrapy.Request(url, self.parse)
        yield title